cmake_minimum_required(VERSION 3.5.1)

if(POLICY CMP0077)
  cmake_policy(SET CMP0077 NEW)
endif()
if(POLICY CMP0116)
  cmake_policy(SET CMP0116 OLD)
endif()

#-------------------------------------------------------------------------------
# Project setup and globals
#-------------------------------------------------------------------------------

project(wafer-compiler LANGUAGES CXX C)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

#-------------------------------------------------------------------------------
# Options and settings
#-------------------------------------------------------------------------------

option(LLVM_INCLUDE_TOOLS "Generate build targets for the LLVM tools." ON)
option(LLVM_BUILD_TOOLS "Build the LLVM tools. If OFF, just generate build targets." ON)

#-------------------------------------------------------------------------------
# MLIR/LLVM Configuration
#-------------------------------------------------------------------------------
if(NOT DEFINED MLIR_DIR)
  set(MLIR_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/llvm/build/lib/cmake/mlir)
endif()

find_package(MLIR REQUIRED CONFIG)
set(LLVM_MLIR_BINARY_DIR ${MLIR_DIR}/../../../bin)
set(LLVM_MLIR_SOURCE_DIR ${MLIR_DIR}/../../../../mlir)

list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")

include(TableGen)
include(AddLLVM)
include(AddMLIR)
include(HandleLLVMOptions)

## GmSSL
if(NOT DEFINED GMSSL_INSTALL_DIR)
  set(GMSSL_INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/GmSSL/build/install)
endif()
include_directories(${GMSSL_INSTALL_DIR}/include/)


## OpenSSL
if(NOT DEFINED OPENSSL_ROOT_DIR)
  set(OPENSSL_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/openssl)
endif()
find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})

#-------------------------------------------------------------------------------
# WAFER configuration
#-------------------------------------------------------------------------------

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)


# Add MLIR and LLVM headers to the include path
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS})

# Add wafer-compiler files to the include path
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include/)
# file(MAKE_DIRECTORY benchmark/build)

option(COMPILE_WITH_SHA1 "Compile with SHA1 support" ON)

include(CheckCXXSourceCompiles)
set(SHA1_SOURCE_CODE "
    #include <immintrin.h> 
    int main() { 
        __m128i data = _mm_set1_epi32(0);
        __m128i result = _mm_sha1msg1_epu32(data, data);
    } 
")
set(CMAKE_REQUIRED_FLAGS "-march=native")
check_cxx_source_compiles("${SHA1_SOURCE_CODE}" SHA1_INSTRUCTION_SUPPORTED)
if(SHA1_INSTRUCTION_SUPPORTED)
  message(STATUS "SHA-1 instructions - supported")
  if(COMPILE_WITH_SHA1)
    message(STATUS "SHA-1 instructions - enabled")
  else()
    set(SHA1_INSTRUCTION_SUPPORTED 0)
    message(STATUS "SHA-1 instructions - disabled (manually)")
  endif()
else()
  message(STATUS "SHA-1 instructions - NOT supported")
endif()

add_subdirectory(lib)
add_subdirectory(include)
add_subdirectory(tools)
# cmake -G Ninja .. -DMLIR_DIR=${MLIR_DIR} -DGMSSL_INSTALL_DIR=${GMSSL_INSTALL_DIR} -DOPENSSL_ROOT_DIR=${OPENSSL_ROOT_DIR} -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_BUILD_TYPE=RELEASE

